import React from 'react';
import { Text, View, InteractionManager } from 'react-native';

import Config from '../../config';
import SizeUtils from '../../common/SizeUtils';
import BaseComponent from './BaseComponent';
import EventManager from '../../events/EventManager';
import JumpToPageByNotification from '../../actions/common/JumpToPageByNotification';

import Orientation from 'react-native-orientation';

export default class BasePage extends BaseComponent {
  constructor(props) {
    super(props);
    this.state.pageName = 'BasePage';
    this.state.loadComplete = false;
    this.state.authorization = 0;
    this.state.refresh = true;
    this.state.leave = false;
    this.state.orientation = 'PORTRAIT';

    this._leave = false;

    this._authorization = this.props.pageData.routeData.authorization;

    this.moduleType = '';
    this.eventDispather = null;

    this.props.pageData.enterPage = this._enterPage.bind(this);
    this.props.pageData.leavePage = this._leavePage.bind(this);
    this.props.pageData.goNotification = this._goNotification.bind(this);

    InteractionManager.runAfterInteractions(() => {
      this._dealAfterEnterAnimation();
    });
  }

  render() {
    if (this.state.leave === true) {
      return this._renderEmptyView();
    }
    return this._dealRender();
  }

  _dealRender() {
    return (
      <View
        style={{
          width: SizeUtils.getWindowSize().width,
          height: SizeUtils.getWindowSize().height
        }}
      >
        {this._renderContent()}
        {this._renderNavigationBar()}
      </View>
    );
  }

  _renderContent() {}

  //添加事件监听
  addEventListener(eventType, method) {
    this._getEventDispatcher().addEventListener(eventType, method);
  }

  //移除事件监听
  removeEventListener(eventType, method) {
    this._getEventDispatcher().removeEventListener(eventType, method);
  }

  //派发
  dispatcherEvent(event) {
    this._getEventDispatcher().dispatcherEvent(event);
  }

  _enterPage() {
    console.log('重新渲染', this.props.pageData, 'leave', this._leave);
    if (this._leave) {
      this._leave = false;
      this.setState({
        refresh: true,
        leave: false
      });
      this._dealEnterPage();
    }
    InteractionManager.runAfterInteractions(() => {
      this._dealAfterEnterAnimation();
    });
  }

  _dealAfterEnterAnimation() {}

  _dealEnterPage() {}

  _leavePage() {
    console.log('离开page', this.props.pageData, 'leave', this._leave);
    if (this._leave == false) {
      this._leave = true;
      this.setState({
        refresh: false,
        leave: true
      });
      this._dealLeavePage();
    }
  }

  _dealLeavePage() {}

  _goNotification(data) {
    new JumpToPageByNotification().call(this, data);
  }

  _getEventDispatcher() {
    if (this.eventDispather == null) {
      this.eventDispather = EventManager.getInstance().getDispather(this.moduleType);
    }
    return this.eventDispatherl;
  }
}
